package com.ecnu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ecnu.common.core.domain.AjaxResult;
import com.ecnu.common.core.domain.entity.SysDictData;
import com.ecnu.common.utils.StringUtils;
import com.ecnu.system.domain.ProjectFile;
import com.ecnu.system.domain.ProjectFileLabels;
import com.ecnu.system.domain.dto.FileLabelDTO;
import com.ecnu.system.domain.dto.FileLabelsDTO;
import com.ecnu.system.domain.vo.FileLabelVO;
import com.ecnu.system.mapper.ProjectFileLabelsMapper;
import com.ecnu.system.mapper.ProjectFileMapper;
import com.ecnu.system.mapper.SysDictDataMapper;
import com.ecnu.system.service.IProjectFileLabelsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * ecnu_活动_资料标签表 服务实现类
 * </p>
 *
 * @author zdn
 * @since 2025-03-23
 */
@Service
public class ProjectFileLabelsServiceImpl extends ServiceImpl<ProjectFileLabelsMapper, ProjectFileLabels> implements IProjectFileLabelsService {
    @Autowired
    private ProjectFileLabelsMapper fileLabelsMapper;
    @Autowired
    private ProjectFileMapper projectFileMapper;
    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Override
    public AjaxResult getList(String id) {
        ProjectFile file = projectFileMapper.selectOne(
                new QueryWrapper<ProjectFile>()
                        .eq("id", id)
                        .eq("del_flag", "0"));
        if (file == null) {
            return AjaxResult.error("文件不存在");
        }
        ArrayList<String> fileList = new ArrayList<>();
        fileList.add(id);
        List<FileLabelVO> file_id = fileLabelsMapper.labelsList(fileList);
        return AjaxResult.success(file_id);
    }

    @Override
    public AjaxResult edit(FileLabelDTO dtos) {
        QueryWrapper<ProjectFile> eq = new QueryWrapper<ProjectFile>()
                .eq("id", dtos.getFileId())
                .eq("del_flag", "0");
        ProjectFile file = projectFileMapper.selectOne(eq);
        if (file == null) {
            return AjaxResult.error("文件不存在");
        }
        fileLabelsMapper.deleteByFileId(dtos.getFileId());
        //获取标签id为空的值
        List<FileLabelsDTO> collect = dtos.getFileLabels()
                .stream()
                .filter(item -> item.getDictCode() == null)
                .collect(Collectors.toList());
        List<FileLabelsDTO> fileLabelsList = dtos.getFileLabels()
                .stream()
                .filter(item -> item.getDictCode() != null)
                .collect(Collectors.toList());
        //添加到数据库
        if (!collect.isEmpty()) {
            for (FileLabelsDTO aLong : collect) {
                if (StringUtils.isNotBlank(aLong.getDictValue())) {
                    SysDictData sysDictData = new SysDictData();
                    sysDictData.setDictValue(aLong.getDictValue());
                    sysDictData.setDictLabel(aLong.getDictValue());
                    sysDictData.setDictType("pro_file_label");
                    sysDictData.setDictSort(1L);
                    sysDictData.setStatus("0");
                    sysDictData.setListClass("default");
                    dictDataMapper.insertDictData(sysDictData);
                    aLong.setDictCode(sysDictData.getDictCode());
                    fileLabelsList.add(aLong);
                }
            }
        }

        dtos.setFileLabels(fileLabelsList);
        //添加入库
        if (!dtos.getFileLabels().isEmpty()) {
            fileLabelsMapper.insertByFileId(dtos.getFileLabels(), dtos.getFileId());
        }

        return AjaxResult.success("保存成功");
    }

    @Override
    public AjaxResult editList(List<FileLabelDTO> dtos) {
        for (FileLabelDTO fileLabelDTO : dtos) {
            AjaxResult edit = edit(fileLabelDTO);
            if (!edit.isSuccess()) { // 遇到失败立即回滚
                log.error("批量保存失败：" + edit.get("msg").toString());
                return edit;
            }
        }
        return AjaxResult.success("批量保存成功");
    }
}
